package com.lxw.nonghe.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lxw.nonghe.entity.Farmer;
import com.lxw.nonghe.mapper.FarmerMapper;
import com.lxw.nonghe.service.FarmerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 参合农民档案表 服务实现类
 * </p>
 *
 * @author lxw
 * @since 2025-04-17
 */
@Service
public class FarmerServiceImpl extends ServiceImpl<FarmerMapper, Farmer> implements FarmerService {

    @Override
    public Page<Farmer> getFarmerPage(int current, int size, String name, String idCard, Integer familyId, String isRural) {
        Page<Farmer> page = new Page<>(current, size);
        LambdaQueryWrapper<Farmer> wrapper = new LambdaQueryWrapper<>();

        if (StringUtils.isNotBlank(name)) {
            wrapper.like(Farmer::getName, name);
        }
        if (StringUtils.isNotBlank(idCard)) {
            wrapper.eq(Farmer::getIdCard, idCard);
        }
        if (familyId != null) {
            wrapper.eq(Farmer::getFamilyId, familyId);
        }
        if (StringUtils.isNotBlank(isRural)) {
            wrapper.eq(Farmer::getIsRural, isRural);
        }

        return this.page(page, wrapper);
    }

    @Override
    public Page<Farmer> getFarmerPage(int current, int size, String name) {
        // 调用已有的方法实现复用
        return getFarmerPage(current, size, name, null, null, null);
    }

    @Override
    public List<Farmer> getFarmersByFamilyId(Integer familyId) {
        QueryWrapper<Farmer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("family_id", familyId);
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public Farmer getFarmerByName(String name) {
        return this.lambdaQuery().eq(Farmer::getName, name).one();
    }

    @Override
    public Farmer getFarmerByIdCart(String idCard) {
        return this.lambdaQuery().eq(Farmer::getIdCard, idCard).one();
    }

    @Override
    public Page<Farmer> queryFarmers(Farmer queryFarmer, int current, int size) {
        LambdaQueryWrapper<Farmer> wrapper = new LambdaQueryWrapper<>();

        // 精确匹配条件
        if (queryFarmer.getFarmerId() != null) {
            wrapper.eq(Farmer::getFarmerId, queryFarmer.getFarmerId());
        }
        if (StringUtils.isNotBlank(queryFarmer.getName())) {
            wrapper.like(Farmer::getName, queryFarmer.getName()); // 使用like实现模糊查询
        }
        if (StringUtils.isNotBlank(queryFarmer.getIdCard())) {
            wrapper.like(Farmer::getIdCard, queryFarmer.getIdCard());
        }
        if (StringUtils.isNotBlank(queryFarmer.getGender())) {
            wrapper.eq(Farmer::getGender, queryFarmer.getGender());
        }
        if (queryFarmer.getBirthDate() != null) {
            wrapper.eq(Farmer::getBirthDate, queryFarmer.getBirthDate());
        }
        if (StringUtils.isNotBlank(queryFarmer.getEthnicity())) {
            wrapper.eq(Farmer::getEthnicity, queryFarmer.getEthnicity());
        }
        if (StringUtils.isNotBlank(queryFarmer.getHealthStatus())) {
            wrapper.like(Farmer::getHealthStatus, queryFarmer.getHealthStatus());
        }
        if (StringUtils.isNotBlank(queryFarmer.getIsRural())) {
            wrapper.eq(Farmer::getIsRural, queryFarmer.getIsRural());
        }
        if (StringUtils.isNotBlank(queryFarmer.getContactNumber())) {
            wrapper.like(Farmer::getContactNumber, queryFarmer.getContactNumber());
        }
        if (StringUtils.isNotBlank(queryFarmer.getPermanentAddress())) {
            wrapper.like(Farmer::getPermanentAddress, queryFarmer.getPermanentAddress());
        }
        if (queryFarmer.getFamilyId() != null) {
            wrapper.eq(Farmer::getFamilyId, queryFarmer.getFamilyId());
        }
        // 排序条件（可根据需求调整）
        wrapper.orderByDesc(Farmer::getFarmerId);
        return this.page(new Page<>(current, size), wrapper);
    }

    @Override
    public String getIdCardByFamilyIdAndName(Integer familyId, String name) {
        Farmer farmer = this.lambdaQuery()
                .eq(Farmer::getFamilyId, familyId)
                .eq(Farmer::getName, name)
                .one();
        return farmer != null ? farmer.getIdCard() : null;
    }
}
